package com.bkgy.modular.tbdsjthemedata.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.calsidata.entity.TbEdge;
import com.bkgy.modular.calsidata.entity.TbFalt;
import com.bkgy.modular.datacollect_bx.bean.*;
import com.bkgy.modular.datacollect_bx.dao.JColdPdo621Dao;
import com.bkgy.modular.datacollect_bx.dao.JColdPdo622Dao;
import com.bkgy.modular.qms.dao.QmsColdPdiDao651652;
import com.bkgy.modular.qms.entity.QmsColdPdiMongoDB;
import com.bkgy.modular.qms.entity.QmsCurveDataMongo;
import com.bkgy.modular.tbdsjprocessrulelibrary.dao.TbDsjProcessRuleLibraryDao;
import com.bkgy.modular.tbdsjprocessrulelibrary.entity.TbDsjProcessRuleLibrary;
import com.bkgy.modular.tbdsjthemedata.dao.TbDsjThemeDataDao;
import com.bkgy.modular.tbdsjthemedata.entity.*;
import com.bkgy.modular.tbdsjthemedata.service.TbDsjThemeDataService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 主题数据表接口具体实现类
 *
 * @author 生成器生成
 * @date 2022-08-01 15:15:42
 */
@Service
@Slf4j
public class TbDsjThemeDataServiceImpl implements TbDsjThemeDataService {

    @Resource
    private TbDsjThemeDataDao tbDsjThemeDataDao;
    @Resource
    private TbDsjProcessRuleLibraryDao tbDsjProcessRuleLibraryDao;
    @Resource
    private MongoTemplate mongoTemplate;//注入自带的查询工具
    @Resource
    private JColdPdo621Dao jColdPdo621Dao;
    @Resource
    private JColdPdo622Dao jColdPdo622Dao;
    @Resource
    private QmsColdPdiDao651652 qmsColdPdiDao651652;

    /**
     * 添加记录
     *
     * @param data   json字符串
     * @param userId 用户id
     * @param sname  用户姓名
     */
    public ResultData insertDataTbDsjThemeData(String data, Long userId, String sname) {
        try {
            JTbDsjThemeData jtbDsjThemeData = JSON.parseObject(data, JTbDsjThemeData.class);
            TbDsjThemeData tbDsjThemeData = jtbDsjThemeData.getTbDsjThemeData();
            CodiUtil.newRecord(userId, sname, tbDsjThemeData);
            tbDsjThemeDataDao.insertDataTbDsjThemeData(tbDsjThemeData);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null);
        }
    }

    /**
     * 根据主键删除对象
     *
     * @param //indocno 对象主键
     */
    public ResultData deleteDataTbDsjThemeDataOne(String theme_group_id, String theme_variable) {
        try {
            tbDsjThemeDataDao.deleteDataTbDsjThemeDataOne(theme_group_id, theme_variable);
            return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 根据主键字符串删除多个对象
     *
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDsjThemeDataMany(String str_id) {
        try {
            String sql = "delete tb_dsj_theme_data where indocno in(" + str_id + ")";
            tbDsjThemeDataDao.deleteDataTbDsjThemeDataMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 修改记录
     *
     * @param userId 用户id
     * @param sname  用户姓名
     * @param data   sysUser 对象实体
     */
    public ResultData updateDataTbDsjThemeData(String data, Long userId, String sname) {
        try {
            JTbDsjThemeData jtbDsjThemeData = JSON.parseObject(data, JTbDsjThemeData.class);
            TbDsjThemeData tbDsjThemeData = jtbDsjThemeData.getTbDsjThemeData();
            CodiUtil.editRecord(userId, sname, tbDsjThemeData);
            tbDsjThemeDataDao.updateDataTbDsjThemeData(tbDsjThemeData);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 分页查看记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjThemeDataByPage(String data) {
        try {
            JTbDsjThemeData jtbDsjThemeData = JSON.parseObject(data, JTbDsjThemeData.class);
            JSONObject jsonObject = null;
            Integer pageIndex = jtbDsjThemeData.getPageIndex();
            Integer pageSize = jtbDsjThemeData.getPageSize();

            if (null == pageIndex || null == pageSize) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            if (null != jtbDsjThemeData.getCondition()) {
                jsonObject = JSON.parseObject(jtbDsjThemeData.getCondition().toString());
            }

            String theme_variable_description = null;
            if (!StringUtils.isEmpty(jsonObject.get("theme_variable_description"))) {
                theme_variable_description = jsonObject.get("theme_variable_description").toString();
            }

            String theme_group_id = null;
            if (!StringUtils.isEmpty(jsonObject.get("theme_group_id"))) {
                theme_group_id = jsonObject.get("theme_group_id").toString();
            }

            String theme_variable = null;
            if (!StringUtils.isEmpty(jsonObject.get("theme_variable"))) {
                theme_variable = jsonObject.get("theme_variable").toString();
            }

            List<TbDsjThemeData> list = tbDsjThemeDataDao.findDataTbDsjThemeDataByPage((pageIndex - 1) * pageSize, pageIndex * pageSize, theme_variable_description, theme_group_id, theme_variable);
            Integer count = tbDsjThemeDataDao.findDataTbDsjThemeDataByPageSize(theme_variable_description, theme_group_id, theme_variable);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 根据主键查询单条记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjThemeDataByIndocno(String data) {
        try {
            JTbDsjThemeData jtbDsjThemeData = JSON.parseObject(data, JTbDsjThemeData.class);
            Long indocno = jtbDsjThemeData.getIndocno();

            TbDsjThemeData tbDsjThemeData = tbDsjThemeDataDao.findDataTbDsjThemeDataByIndocno(indocno);
            return ResultData.ResultDataSuccess(tbDsjThemeData);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 查看记录
     *
     * @return list 对象集合返回
     */
    public List<TbDsjThemeData> findDataTbDsjThemeData() {
        List<TbDsjThemeData> list = tbDsjThemeDataDao.findDataTbDsjThemeData();
        return list;
    }

    @Override
    public ResultData findDataTbDsjThemeDataByfind_atacollect622(String data) {
        try {
            Inputmodel inputmodel = JSON.parseObject(data, Inputmodel.class);
            String theme_group_id = inputmodel.getTheme_group_id();  //主题id
            String extid = inputmodel.getExtid();                 //卷号

            Map<String, Object> mapall = new HashMap<>();
            Map<String, List<Double>> mapa_1 = new HashMap<>();//mongdb   曲线
            Map<String, Object> mapa_1_1 = new HashMap<>();//mongdb   点

            Map<String, List<TbDsjThemeData>> mapa_2 = new HashMap<>();//db2
            //String THEME_GROUP_ID="plan_check";//主题id
            List<Tbmodel> list_them = tbDsjThemeDataDao.findDataThemlist(theme_group_id);

            //分组俩个
            Map<String, Map<String, List<Tbmodel>>> test_list_2 = list_them.stream().collect(Collectors.groupingBy(Tbmodel::getIN_DATA_SOURCE, Collectors.groupingBy(Tbmodel::getPRODUCTION_LINE)));
            test_list_2.forEach((key1, value1) -> {
                if (("5").equals(key1)) {
                    //取出数据源为5的，mongdbb
                    Map<String, List<Tbmodel>> f1 = value1; //对数据源分组后
                    Query query = new Query(Criteria.where("extid").is(extid));
                    List<JDataSave622> list_622 = mongoTemplate.find(query, JDataSave622.class);
                    f1.forEach((key2, value2) -> {
                        if (key2.equals("C622")) {
                            for (JDataSave622 entity1 : list_622) {
                                //循环List<Tbmodel> f2
                                for (Tbmodel entityF2 : value2) {
                                    if (("2").equals(entityF2.getDic_variable_property())) {
                                        //为曲线
                                        Map<String, List<Double>> M2 = entity1.getRow();
                                        M2.forEach((key, value) -> {
                                            if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                mapa_1.put(key, value);
                                            }
                                        });
                                    } else {
                                        //为点 1为点
                                        Map<String, Double> M2 = entity1.getTrailValues();
                                        M2.forEach((key, value) -> {
                                            if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                mapa_1_1.put(key, value);
                                            }
                                        });
                                    }
                                }
                            }

                        }
                    });

                }
            });
            ////////////////////////////////
            Map<String, Object> mapamon1 = new HashMap<>();
            mapamon1.put("mongdian", mapa_1_1);  //点
            mapamon1.put("mongxian", mapa_1);  //曲线
            mapall.put("mongdb", mapamon1);


            return ResultData.ResultDataSuccess(mapall, 0);
            //return ResultData.ResultDataSuccess(list_them, 0);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData findDataTbDsjThemeDataByfind_model(String data) {
        log.error("data:" + data);
        long start = System.currentTimeMillis();
        try {
            Inputmodel inputmodel = JSON.parseObject(data, Inputmodel.class);
            String theme_group_id = inputmodel.getTheme_group_id();  //主题id
            String extid = inputmodel.getExtid();                 //卷号

            Map<String, Object> mapall = new HashMap<>();
            Map<String, Object> mapa_1 = new HashMap<>();//mongdb   曲线
            Map<String, Object> mapa_1_1 = new HashMap<>();//mongdb   点

            Map<String, List<TbDsjThemeData>> mapa_2 = new HashMap<>();//db2
            //String THEME_GROUP_ID="plan_check";//主题id
            List<Tbmodel> list_them = tbDsjThemeDataDao.findDataThemlist(theme_group_id);
            ////////////////////////////////
            //分组俩个
            Map<String, Map<String, List<Tbmodel>>> test_list_2 = list_them.stream().collect(Collectors.groupingBy(Tbmodel::getIN_DATA_SOURCE, Collectors.groupingBy(Tbmodel::getPRODUCTION_LINE)));

            test_list_2.forEach((key1, value1) -> {
                if (("5").equals(key1)) {
                    //取出数据源为5的，mongdbb
                    Map<String, List<Tbmodel>> f1 = value1; //对数据源分组后
                    //查询三个表数据归档
                    Query query = new Query(Criteria.where("extid").is(extid));
                    List<JDataSave621> list_621 = mongoTemplate.find(query, JDataSave621.class);
                    List<JDataSave622> list_622 = mongoTemplate.find(query, JDataSave622.class);
                    List<JDataSave642> list_642 = mongoTemplate.find(query, JDataSave642.class);
                    List<JDataSave653> list_653 = mongoTemplate.find(query, JDataSave653.class);
                    List<TbEdge> list_edge = mongoTemplate.find(query, TbEdge.class);
                    /*///pdi三合一  查询pdi时，需要先去宝信db2表把extid转华为entid
					T_dpdo_data t_dpdo_data = tbDsjThemeDataDao.find_entid(extid);
					Query query1=new Query(Criteria.where("volume").is(t_dpdo_data.getEntid()));
					List<QmsColdPdiMongoDB> list_pdi = mongoTemplate.find(query1, QmsColdPdiMongoDB.class);*/

                    ///三个 pdo
                    Query query2 = new Query(Criteria.where("volumeNo").is(extid));
                    List<JColdPdoMongo621> list_621pdo = mongoTemplate.find(query2, JColdPdoMongo621.class);
                    List<JColdPdoMongo622> list_622pdo = mongoTemplate.find(query2, JColdPdoMongo622.class);
                    List<JColdPdoMongo642> list_642pdo = mongoTemplate.find(query2, JColdPdoMongo642.class);

                    if (theme_group_id.contains("pdoandpdi")) {
                        f1.forEach((key2, value2) -> {
                            List<QmsColdPdiMongoDB> list_pdi = new ArrayList<>();
                            List<QmsColdPdiMongoDB> list_pdo = new ArrayList<>();
                            if (key2.contains("C621")) {
                                list_pdi = protea(theme_group_id, extid, list_pdi, "C621");
                                pdo621(mapa_1_1, list_621pdo, value2);
                                //添加查询归档，
                                if (key2.equals("C621")) {
                                    for (JDataSave621 entity1 : list_621) {
                                        //循环List<Tbmodel> f2
                                        for (Tbmodel entityF2 : value2) {
                                            if (("2").equals(entityF2.getDic_variable_property())) {
                                                //为曲线
                                                Map<String, List<Double>> M2 = entity1.getRow();
                                                M2.forEach((key, value) -> {
                                                    if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                        mapa_1.put(key, value);
                                                    }
                                                });
                                            } else {
                                                //为点 1为点
                                                Map<String, Double> M2 = entity1.getTrailValues();
                                                M2.forEach((key, value) -> {
                                                    if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                        mapa_1_1.put(key, value);
                                                    }
                                                });
                                            }
                                        }
                                    }
                                }

                            } else if (key2.contains("C622")) {
                                list_pdi = protea(theme_group_id, extid, list_pdi, "C622");
                                for (JColdPdoMongo622 entity1 : list_622pdo) {
                                    //循环List<Tbmodel> value2
                                    for (Tbmodel entityF2 : value2) {
                                        //为点 1为点
                                        Map<String, Object> M2 = entity1.getRow();
                                        M2.forEach((key, value) -> {
                                            if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                mapa_1_1.put(key, value);
                                            }
                                        });
                                    }
                                }
                                //添加查询归档，
                                if (key2.equals("C622")) {
                                    for (JDataSave622 entity1 : list_622) {
                                        //循环List<Tbmodel> f2
                                        for (Tbmodel entityF2 : value2) {
                                            if (("2").equals(entityF2.getDic_variable_property())) {
                                                //为曲线
                                                Map<String, List<Double>> M2 = entity1.getRow();
                                                M2.forEach((key, value) -> {
                                                    if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                        mapa_1.put(key, value);
                                                    }
                                                });
                                            } else {
                                                //为点 1为点
                                                Map<String, Double> M2 = entity1.getTrailValues();
                                                M2.forEach((key, value) -> {
                                                    if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                        mapa_1_1.put(key, value);
                                                    }
                                                });
                                            }
                                        }
                                    }
                                }
                            } else if (key2.contains("C642")) {
                                list_pdi = protea(theme_group_id, extid, list_pdi, "C642");
                                pdo642(mapa_1_1, list_642pdo, value2);
                                //添加查询归档，
                                if (key2.equals("C642")) {
                                    for (JDataSave642 entity1 : list_642) {
                                        //循环List<Tbmodel> f2
                                        for (Tbmodel entityF2 : value2) {
                                            if (("2").equals(entityF2.getDic_variable_property())) {
                                                //为曲线
                                                Map<String, List<Double>> M2 = entity1.getRow();
                                                M2.forEach((key, value) -> {
                                                    if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                        mapa_1.put(key, value);
                                                    }
                                                });
                                            } else {
                                                //为点 1为点
                                                Map<String, Double> M2 = entity1.getTrailValues();
                                                M2.forEach((key, value) -> {
                                                    if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                        mapa_1_1.put(key, value);
                                                    }
                                                });
                                            }
                                        }
                                    }
                                }
                            }
                            formapa1_1(value2, list_pdi, mapa_1_1);
                        });
                    }
//                    else if (theme_group_id.contains("Si_Steel_Curve")) {
//                        //获得曲线图
//                        f1.forEach((key2, value2) -> {
//                            if (key2.contains("C621")) {
//                                for (TbEdge entity : list_edge) {
//                                    //为曲线
//                                    Map<String, Object> M2 = entity.getEdge();
//                                    M2.forEach((key, value) -> {
//                                        List<Double> ob = CodiUtil.castList(value, Double.class);
//                                        mapa_1.put(key, ob);
//                                    });
//                                }
//                            } else if (key2.contains("C622")) {
//
//                            } else {
//
//                            }
//                        });
//                    }
                    else {
                        //分别找出 三个表 C621、C622、C642
                        f1.forEach((key2, value2) -> {
                            if (key2.equals("C621")) {
                                for (JDataSave621 entity1 : list_621) {
                                    //循环List<Tbmodel> f2
                                    for (Tbmodel entityF2 : value2) {
                                        if (("2").equals(entityF2.getDic_variable_property())) {
                                            //为曲线
                                            Map<String, List<Double>> M2 = entity1.getRow();
                                            mapa_1_1.put("start", entity1.getStart());
                                            mapa_1_1.put("end", entity1.getEnd());
                                            M2.forEach((key, value) -> {
                                                if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                    mapa_1.put(key, value);
                                                }
                                            });
                                        } else {
                                            //为点 1为点
                                            Map<String, Double> M2 = entity1.getTrailValues();
                                            M2.forEach((key, value) -> {
                                                if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                    mapa_1_1.put(key, value);
                                                }
                                            });
                                        }
                                    }
                                }
                            }
                            else if (key2.equals("C622")) {
                                for (JDataSave622 entity1 : list_622) {
                                    //循环List<Tbmodel> f2
                                    for (Tbmodel entityF2 : value2) {
                                        if (("2").equals(entityF2.getDic_variable_property())) {
                                            //为曲线
                                            Map<String, List<Double>> M2 = entity1.getRow();
                                            mapa_1_1.put("start", entity1.getStart());
                                            mapa_1_1.put("end", entity1.getEnd());
                                            M2.forEach((key, value) -> {
                                                if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                    mapa_1.put(key, value);
                                                }
                                            });
                                        }
                                        else {
                                            //为点 1为点
                                            Map<String, Double> M2 = entity1.getTrailValues();
                                            M2.forEach((key, value) -> {
                                                if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                    mapa_1_1.put(key, value);
                                                }
                                            });
                                        }
                                    }
                                }
                            }
                            else if (key2.equals("C642")) {
                                for (JDataSave642 entity1 : list_642) {
                                    //循环List<Tbmodel> f2
                                    for (Tbmodel entityF2 : value2) {
                                        if (("2").equals(entityF2.getDic_variable_property())) {
                                            //为曲线
                                            Map<String, List<Double>> M2 = entity1.getRow();
                                            mapa_1_1.put("start", entity1.getStart());
                                            mapa_1_1.put("end", entity1.getEnd());
                                            M2.forEach((key, value) -> {
                                                if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                    mapa_1.put(key, value);
                                                }
                                            });
                                        } else {
                                            //为点 1为点
                                            Map<String, Double> M2 = entity1.getTrailValues();
                                            M2.forEach((key, value) -> {
                                                if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                    mapa_1_1.put(key, value);
                                                }
                                            });
                                        }
                                    }
                                }

                            }
                            else if (key2.equals("C653")) {
                                for (JDataSave653 entity1 : list_653) {
                                    //循环List<Tbmodel> f2
                                    for (Tbmodel entityF2 : value2) {
                                        if (("2").equals(entityF2.getDic_variable_property())) {
                                            //为曲线
                                            Map<String, List<Double>> M2 = entity1.getRow();
                                            mapa_1_1.put("start", entity1.getStart());
                                            mapa_1_1.put("end", entity1.getEnd());
                                            M2.forEach((key, value) -> {
                                                if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                    mapa_1.put(key, value);
                                                }
                                            });
                                        } else {
                                            //为点 1为点
                                            Map<String, Double> M2 = entity1.getTrailValues();
                                            M2.forEach((key, value) -> {
                                                if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                    mapa_1_1.put(key, value);
                                                }
                                            });
                                        }
                                    }
                                }

                            }
                            else if (key2.equals("C621_PDI") || key2.equals("C622_PDI") || key2.equals("C642_PDI")) {
                                ///pdi三合一  查询pdi时，需要先去宝信db2表把extid转华为entid
                                log.info("进入pdi");
                                List<QmsColdPdiMongoDB> list_pdi = new ArrayList<>();
                                if (theme_group_id.equals("c642_pdo")) {
                                    list_pdi = protea(theme_group_id, extid, list_pdi, "C642");
                                } else {
                                    log.info("主题组不是c642_pdo");
                                    Query querya = new Query(Criteria.where("extid").is(extid));
                                    list_pdi = mongoTemplate.find(querya, QmsColdPdiMongoDB.class);
                                    System.out.print(list_pdi);
                                }
                                formapa1_1(value2, list_pdi, mapa_1_1);
                            }
                            else if (key2.equals("C621_PDO")) {
                                pdo621(mapa_1_1, list_621pdo, value2);
                            }
                            else if (key2.equals("C622_PDO")) {
                                for (JColdPdoMongo622 entity1 : list_622pdo) {
                                    //循环List<Tbmodel> value2
                                    for (Tbmodel entityF2 : value2) {
                                        //为点 1为点
                                        Map<String, Object> M2 = entity1.getRow();
                                        M2.forEach((key, value) -> {
                                            if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                mapa_1_1.put(key, value);
                                            }
                                        });
                                    }
                                }
                            }
                            else if (key2.equals("C642_PDO")) {
                                pdo642(mapa_1_1, list_642pdo, value2);
                            }
                        });
                    }
                }
                else if (("3").equals(key1)) {
                    changeThree(value1, mapa_1_1);
                }
                else if (("6").equals(key1)) {
                    //取出数据源为5的，mongdbb
                    Map<String, List<Tbmodel>> f1 = value1; //对数据源分组后

                    //查询三个表数据归档
                    Query query = new Query(Criteria.where("extid").is(extid));
                    List<TbEdge> tbEdgeList = mongoTemplate.find(query, TbEdge.class);
                    //分别找出 三个表 C621、C622、C642
                    f1.forEach((key2, value2) -> {
                        if (key2.equals("C621")) {
                            for (TbEdge entity1 : tbEdgeList) {
                                //循环List<Tbmodel> f2
                                for (Tbmodel entityF2 : value2) {
                                    if (("2").equals(entityF2.getDic_variable_property())) {
                                        //为曲线
                                        Map<String, Object> M2 = entity1.getEdge();
                                        M2.forEach((key, value) -> {
                                            if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                mapa_1.put(key, value);
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    });
                }
                else if (("7").equals(key1)) {
                    //取出数据源为5的，mongdbb
                    Map<String, List<Tbmodel>> f1 = value1; //对数据源分组后

                    //查询三个表数据归档
                    Query query = new Query(Criteria.where("extid").is(extid));
                    List<TbFalt> tbFaltList = mongoTemplate.find(query, TbFalt.class);
                    //分别找出 三个表 C621、C622、C642
                    f1.forEach((key2, value2) -> {
                        if (key2.equals("C622")) {
                            for (TbFalt entity1 : tbFaltList) {
                                //循环List<Tbmodel> f2
                                for (Tbmodel entityF2 : value2) {
                                    if (("2").equals(entityF2.getDic_variable_property())) {
                                        //为曲线
                                        Map<String, Object> M2 = entity1.getEdge();
                                        M2.forEach((key, value) -> {
                                            if (key.equals(entityF2.getTHEME_VARIABLE())) {
                                                mapa_1.put(key, value);
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    });
                }
            });
            ////////////////////////////////
            List<TbDsjThemeData> list_td = tbDsjThemeDataDao.findDataTbDsjThemeDataByPage(0, 10, null, null, null);

            Map<String, Object> mapamon1 = new HashMap<>();
            mapamon1.put("mongdian", mapa_1_1);  //点
            mapamon1.put("mongxian", mapa_1);  //曲线

            mapall.put("mongdb", mapamon1);
            //mapall.put("mongdb",mapa_1);
            mapa_2.put("list1_db2", list_td);
            mapall.put("db2", mapa_2);

            long end = System.currentTimeMillis();
            log.error("开始时间: " + start);
            log.error("结束时间: " + end);
            log.error("耗时: " + (end - start) / 1000);
            return ResultData.ResultDataSuccess(mapall, 0);
            //return ResultData.ResultDataSuccess(list_them, 0);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    private void changeThree(Map<String, List<Tbmodel>> value1, Map<String, Object> mapa_1_1) {
        //取出数据源为3的，db2
        value1.forEach((key2, value2) -> {
            for (Tbmodel entity : value2) {
                String theme_variable = entity.getTHEME_VARIABLE();
                List<TbDsjProcessRuleLibrary> libraryList = tbDsjProcessRuleLibraryDao.findDataTbDsjProcessRuleLibraryNoPage(theme_variable, null);
                Double threshold_value = libraryList.get(0).getThreshold_value();
                mapa_1_1.put(theme_variable, threshold_value);
            }
        });
    }

    private void pdo621(Map<String, Object> mapa_1_1, List<JColdPdoMongo621> list_621pdo, List<Tbmodel> value2) {
        for (JColdPdoMongo621 entity1 : list_621pdo) {
            //循环List<Tbmodel> value2
            for (Tbmodel entityF2 : value2) {
                //为点 1为点
                Map<String, Object> M2 = entity1.getRow();
                M2.forEach((key, value) -> {
                    if (key.equals(entityF2.getTHEME_VARIABLE())) {
                        mapa_1_1.put(key, value);
                    }
                });
            }
        }
    }

    private void pdo642(Map<String, Object> mapa_1_1, List<JColdPdoMongo642> list_642pdo, List<Tbmodel> value2) {
        for (JColdPdoMongo642 entity1 : list_642pdo) {
            //循环List<Tbmodel> value2
            for (Tbmodel entityF2 : value2) {
                //为点 1为点
                Map<String, Object> M2 = entity1.getRow();
                M2.forEach((key, value) -> {
                    if (key.equals(entityF2.getTHEME_VARIABLE())) {
                        mapa_1_1.put(key, value);
                    }
                });
            }
        }
    }

    private List<QmsColdPdiMongoDB> protea(String theme_group_id, String extid, List<QmsColdPdiMongoDB> list_pdi, String line) {
        log.info("主题组是" + theme_group_id + "，出口卷号：" + extid);
        T_dpdo_data t_dpdo_data = new T_dpdo_data();
        if (line.contains("C621")) {
            t_dpdo_data = tbDsjThemeDataDao.find_entid_C621(extid);
        } else if (line.contains("C622")) {
            t_dpdo_data = tbDsjThemeDataDao.find_entid_C622(extid);
        } else {
            t_dpdo_data = tbDsjThemeDataDao.find_entid(extid);
        }
        if (t_dpdo_data != null) {
            if (!StringUtils.isEmpty(t_dpdo_data.getEntid())) {
                Query query1 = new Query(Criteria.where("volume").is(t_dpdo_data.getEntid()));
                list_pdi = mongoTemplate.find(query1, QmsColdPdiMongoDB.class);
                log.info("输出转化的入口卷号：" + t_dpdo_data.getEntid());
                System.out.print(list_pdi);
            }
        } else {
            log.info("主题组是" + theme_group_id + "，出口卷号：" + extid + "无法转化入口卷号");
        }
        return list_pdi;
    }

    private void formapa1_1(List<Tbmodel> value2, List<QmsColdPdiMongoDB> list_pdi, Map<String, Object> mapa_1_1) {
        for (QmsColdPdiMongoDB entity1 : list_pdi) {
            //循环List<Tbmodel> value2
            for (Tbmodel entityF2 : value2) {
                if (!("2").equals(entityF2.getDic_variable_property())) {
                    //为点 1为点
                    Map<String, Object> M2 = entity1.getMap();
                    M2.forEach((key, value) -> {
                        if (key.equals(entityF2.getTHEME_VARIABLE())) {
                            mapa_1_1.put(key, value);
                        }
                    });
                }
            }
        }
    }

    /**
     * 根据卷号和主题查询热轧数据
     *
     * @param data
     * @return
     */
    @Override
    public ResultData findHotData(String data) {
        log.error("findHotData:>>>>" + data);
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            //卷号
            String volumeNo = jsonObject.getString("volumeNo");
            //主题
            String themeGroupId = jsonObject.getString("theme_group_id");
            if (!StringUtils.hasText(volumeNo) || !StringUtils.hasText(themeGroupId)) {
                return ResultData.ResultDataFaultSelf("卷号和主题不能为空", null);
            }
            List<HotModel> hotThem = tbDsjThemeDataDao.findDataHotThem(themeGroupId);
            //点
            HashMap<String, Object> mapPoint = new HashMap<>();
            //线
            HashMap<String, List<Double>> mapLine = new HashMap<>();
            //根据卷号查询qmsCureData数据
            QmsCurveDataMongo qmsCurveDataMongo = null;

            //收到的是621出口卷号，从621DPdo获取入口卷号
            if (themeGroupId.contains("c621_ex")) {
                JColdPdo621 tDPdo = jColdPdo621Dao.selectById(volumeNo);
                volumeNo = tDPdo.getEntid();
                QmsCurveDataMongo qmsLike = findQmsLike(volumeNo);
                if (!ObjectUtils.isEmpty(qmsLike)) {
                    qmsCurveDataMongo = qmsLike;
                } else {
                    volumeNo = getVolumeNoFromQ651652(volumeNo);
                    qmsCurveDataMongo = findQmsLike(volumeNo);
                }
            }

            //收到的是621入口卷号
            if (themeGroupId.contains("c621_en")) {
                QmsCurveDataMongo qmsLike = findQmsLike(volumeNo);
                if (!ObjectUtils.isEmpty(qmsLike)) {
                    qmsCurveDataMongo = qmsLike;
                } else {
                    volumeNo = getVolumeNoFromQ651652(volumeNo);
                    qmsCurveDataMongo = findQmsLike(volumeNo);
                }
            }

            //收到的是622出口卷号，从622DPdo获取入口卷号
            if (themeGroupId.contains("c622_ex")) {
                JColdPdo622 tDPdo = jColdPdo622Dao.selectById(volumeNo);
                if (!ObjectUtils.isEmpty(tDPdo)) {
                    volumeNo = tDPdo.getEntid();
                }
                qmsCurveDataMongo = findQmsLike(volumeNo);
            }

            //收到的是622入口卷号
            if (themeGroupId.contains("c622_en")) {
                qmsCurveDataMongo = findQmsLike(volumeNo);
            }

            if (!CollectionUtils.isEmpty(hotThem) && !ObjectUtils.isEmpty(qmsCurveDataMongo)) {
                for (HotModel hotModel : hotThem) {
                    if (hotModel.getArea_code().equals("PDI")) {
                        Map<String, Object> pdi = qmsCurveDataMongo.getPdi();
                        if (StringUtils.isEmpty(pdi)) {
                            continue;
                        }
                        Object val1 = pdi.get(hotModel.getTheme_variable());
                        mapPoint.put(hotModel.getTheme_variable(), val1);
                    } else if (hotModel.getArea_code().equals("PDO")) {
                        Map<String, Object> pdo = qmsCurveDataMongo.getPdo();
                        if (StringUtils.isEmpty(pdo)) {
                            continue;
                        }
                        Object val2 = pdo.get(hotModel.getTheme_variable());
                        mapPoint.put(hotModel.getTheme_variable(), val2);
                    } else if (hotModel.getArea_code().equals("TZZ")) {
                        Map<String, Double> trailValues = qmsCurveDataMongo.getTrailValues();
                        if (StringUtils.isEmpty(trailValues)) {
                            continue;
                        }
                        Object val2 = trailValues.get(hotModel.getTheme_variable());
                        mapPoint.put(hotModel.getTheme_variable(), val2);
                    } else {
                        Map<String, List<Double>> originBolg = qmsCurveDataMongo.getOriginBolg();
                        if (StringUtils.isEmpty(originBolg)) {
                            continue;
                        }
                        List<Double> data1 = originBolg.get(hotModel.getTheme_variable());
                        mapLine.put(hotModel.getTheme_variable(), data1);
                    }
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("mapLine", mapLine);
            map.put("mapPoint", mapPoint);
            return ResultData.ResultDataSuccess(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 根据卷号查询MongoDb的QMSBlob数据库
     *
     * @param volumeNo
     * @return
     */
    public QmsCurveDataMongo findQmsLike(String volumeNo) {
        if (!StringUtils.hasText(volumeNo)) {
            return null;
        }
        Pattern pattern = Pattern.compile("^" + volumeNo.substring(0, volumeNo.length() - 1) + ".*$", Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("volumeNo").regex(pattern));
        List<QmsCurveDataMongo> list = mongoTemplate.find(query, QmsCurveDataMongo.class);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据出口卷号volumeNo从常化表查询入口卷号
     *
     * @param volumeNo
     * @return
     */
    private String getVolumeNoFromQ651652(String volumeNo) {
        String no651 = qmsColdPdiDao651652.findNo651(volumeNo);
        if (StringUtils.hasText(no651)) {
            return no651;
        }
        String no652 = qmsColdPdiDao651652.findNo652(volumeNo);
        if (StringUtils.hasText(no651)) {
            return no652;
        }
        return null;
    }


}
